$(document).ready(function()
{
  Array.prototype.removeDuplicates = function ()
  {
    var temp=new Array();
    this.sort();
    for (i = 0; i < this.length; i++)
    {
      if (this[i] == this[i + 1]) { continue; }
      temp[temp.length] = this[i];
    }

    return temp;
  } 

  // Prevent form from submitting when I hit enter
  $("form").submit(function() { return false; });
  $("h3").click(function() { $(this).nextAll().toggle(); });
  $("#browse").click(function() { browse(); });
  $("#hideAll").click(function() { hideAll(); });
  $("input").keypress(function(e) { if (enterKeyHit(e)) search(); });

  // Underline last paragraph of each recipe, sort them alphabetically
  // and hide them all.
  var recipes = $(".recipe");
  recipes.each(function() {
    var p = $(this).children("p").last();
    p.css("border-bottom", "1px solid black");
    p.css("padding-bottom", "10px");
  });
  recipes.tsort("h3");
  hideAll();

  addCategoryCheckboxes();
});

function addCategoryCheckboxes()
{
  var classes = getCategories();
  var catElement = $("#categories");
  for (var i = 0; i < classes.length; i++)
  {
    catElement.append("<label for='" + classes[i] + "'>" + classes[i] + "</label> " +
                      "<input type='checkbox' id='" + classes[i] + "' class='categoryCheck'/>");
  }

  $(".categoryCheck").click(function()
  {
    var checked = $(this).attr("checked");
    if (checked)
    {
      $("." + $(this).attr("id")).children("h3").show();
      if ($("#searchTerm").val().trim().length > 0)
        search();
    }

    else
      $("." + $(this).attr("id")).children().hide();
  });
}

function getCategories()
{
  var classes = new Array();
  $(".recipe").each(function()
  {
    classes = classes.concat($(this).attr("class").split(" "));
  });

  classes = classes.removeDuplicates();
  var idx = classes.indexOf("recipe");
  if (idx >= 0);
    classes.splice(idx, 1);

  return classes;
}

function browse()
{
  $(".recipe").children("h3").show();
  $("input").val("");
  $(".categoryCheck").attr("checked", true);
}

function hideAll()
{
  $(".recipe").children().hide();
  $("input").val("");
  $('input[type=checkbox]').attr('checked', false);
}

function getSelectedRecipies()
{
  var recipies = new Array();
  $(".categoryCheck").each(function()
  {
    if ($(this).attr("checked"))
      recipies.push("." + $(this).attr("id"));
  });

  var str = recipies.join(", ").trim();
  if (str.length > 0)
    return $(str);

  else
    return $(".recipe");
}

function search()
{
  $(".recipe").children().hide();

  var termlist = $("#searchTerm").val().split(" ");
  for (var i = 0; i < termlist.length;)
  {
    var term = $.trim(termlist[i]);
    if (term.length > 0)
    {
      termlist[i] = new RegExp(term, "i");
      i++;
    }

    else
      termlist.splice(i, 1);
  }

  getSelectedRecipies().each(function()
  {
    for (var i = 0; i < termlist.length; i++)
    {
      var me   = $(this);
      var text = me.text();
      if (!termlist[i].test(text))
        return;
    }

    me.children("h3").show();
  });
}

function enterKeyHit(evt)
{
  var evt = (evt) ? evt : ((event) ? event : null);
  return (evt.keyCode == 13);
}
